'''
封装request 请求库，
1.添加自己的日志系统
2.添加异常处理
'''

import requests
# import logging
# logging.basicConfig(filename='example.log',level=logging.DEBUG)
from common.logger import logger


class ClientSDK:

    def __init__(self):
        # 设置api 前置路径
        self.base_url = 'http://49.233.108.117:3000/api/v1'

    def do_request(self,method:str,url:str,desc=None,params=None,data=None,**kwargs):
        '''
        统一封装请求处理方法
        :param method:请求方法（get或post）
        :param url:请求路径
        :param desc:描述
        :param params:查询参数
        :param data:请求数据
        :param kwargs:其他参数
        :return:
        '''

        url = self.base_url+url
        # print(f'[{method} {url} {desc}]')
        # logging.info(f'[{method}] {url} {desc}')
        logger.info(f'{method} {url} {desc}')

        if method =='get':
            res = requests.get(url,params=params,**kwargs)
            # print(f'{params} - {res.status_code}')
            # logging.info(f'{params} - {res.status_code}'
            logger.info(f'{params} - {res.status_code}')
        elif method ==  "post":
            res = requests.post(url,data=data,**kwargs)
            # print(f'{data} - {res.status_code}')
            # logging.info(f'{data} - {res.status_code}')
            logger.info(f'{data} - {res.status_code}')
            return res

if __name__ == '__main__':
    sdk = ClientSDK()
    sdk.do_request('get','/topics','主题首页')
    sdk.do_request('post','/topics','新建主题')